#!/bin/bash

# 测试JupyterHub创建和配置的脚本
# 使用方法：./test-jupyterhub.sh <group_id>

if [ $# -ne 1 ]; then
    echo "使用方法: $0 <group_id>"
    echo "示例: $0 test-group-001"
    exit 1
fi

GROUP_ID=$1
CONTAINER_NAME="jhub-$GROUP_ID"
GROUP_DIR="/tmp/practice-platform/shared/$GROUP_ID-jhub"

echo "测试JupyterHub创建和配置..."
echo "组ID: $GROUP_ID"
echo "容器名称: $CONTAINER_NAME"
echo "组目录: $GROUP_DIR"
echo ""

# 1. 检查目录是否存在
echo "1. 检查组目录..."
if [ -d "$GROUP_DIR" ]; then
    echo "✓ 组目录存在: $GROUP_DIR"
    ls -la "$GROUP_DIR"
else
    echo "✗ 组目录不存在: $GROUP_DIR"
fi

echo ""

# 2. 检查配置文件
echo "2. 检查配置文件..."
if [ -f "$GROUP_DIR/jupyterhub_config.py" ]; then
    echo "✓ JupyterHub配置文件存在"
    echo "配置文件内容预览:"
    head -20 "$GROUP_DIR/jupyterhub_config.py"
else
    echo "✗ JupyterHub配置文件不存在"
fi

echo ""

# 3. 检查Docker Compose文件
echo "3. 检查Docker Compose文件..."
if [ -f "$GROUP_DIR/docker-compose.yml" ]; then
    echo "✓ Docker Compose文件存在"
    echo "Compose文件内容预览:"
    head -15 "$GROUP_DIR/docker-compose.yml"
else
    echo "✗ Docker Compose文件不存在"
fi

echo ""

# 4. 检查容器状态
echo "4. 检查容器状态..."
if docker ps | grep -q "$CONTAINER_NAME"; then
    echo "✓ 容器正在运行: $CONTAINER_NAME"
    docker ps | grep "$CONTAINER_NAME"
    
    # 获取端口映射
    PORT=$(docker port "$CONTAINER_NAME" 8000/tcp | cut -d: -f2)
    if [ ! -z "$PORT" ]; then
        echo "✓ 容器端口映射: 8000 -> $PORT"
        
        # 测试健康检查
        echo "5. 测试JupyterHub健康检查..."
        HEALTH_URL="http://127.0.0.1:$PORT/hub/health"
        echo "健康检查URL: $HEALTH_URL"
        
        if curl -s -f "$HEALTH_URL" > /dev/null; then
            echo "✓ JupyterHub服务健康"
        else
            echo "✗ JupyterHub服务不健康"
        fi
        
        # 测试登录页面
        echo "6. 测试登录页面..."
        LOGIN_URL="http://127.0.0.1:$PORT/jhub/hub/login"
        echo "登录页面URL: $LOGIN_URL"
        
        if curl -s -f "$LOGIN_URL" > /dev/null; then
            echo "✓ 登录页面可访问"
        else
            echo "✗ 登录页面不可访问"
        fi
        
    else
        echo "✗ 无法获取端口映射"
    fi
    
else
    echo "✗ 容器未运行: $CONTAINER_NAME"
    
    # 检查容器是否存在但已停止
    if docker ps -a | grep -q "$CONTAINER_NAME"; then
        echo "容器存在但已停止:"
        docker ps -a | grep "$CONTAINER_NAME"
        
        echo "尝试启动容器..."
        cd "$GROUP_DIR" && docker-compose up -d
        
        if [ $? -eq 0 ]; then
            echo "✓ 容器启动成功"
            sleep 10
            echo "等待容器就绪..."
            
            # 再次检查健康状态
            PORT=$(docker port "$CONTAINER_NAME" 8000/tcp | cut -d: -f2)
            if [ ! -z "$PORT" ]; then
                HEALTH_URL="http://127.0.0.1:$PORT/hub/health"
                echo "健康检查URL: $HEALTH_URL"
                
                for i in {1..30}; do
                    if curl -s -f "$HEALTH_URL" > /dev/null; then
                        echo "✓ JupyterHub服务已就绪 (尝试 $i/30)"
                        break
                    fi
                    echo "等待服务就绪... (尝试 $i/30)"
                    sleep 2
                done
            fi
        else
            echo "✗ 容器启动失败"
        fi
    fi
fi

echo ""
echo "测试完成！"

# 显示有用的命令
echo ""
echo "有用的命令:"
echo "查看容器日志: docker logs $CONTAINER_NAME"
echo "进入容器: docker exec -it $CONTAINER_NAME bash"
echo "重启容器: cd $GROUP_DIR && docker-compose restart"
echo "停止容器: cd $GROUP_DIR && docker-compose down"
echo "查看端口: docker port $CONTAINER_NAME" 